package com.feiyuan.book.service.impl;

import com.feiyuan.book.entity.BookChapterEntity;
import com.feiyuan.book.entity.BookListEntity;
import com.feiyuan.book.service.BookChapterService;
import com.feiyuan.book.service.BookListService;
import com.feiyuan.book.vo.BookChapterVO;
import com.feiyuan.book.vo.BookContentVO;
import com.feiyuan.book.webmagic.bqg.processor.BookContentProcessor;
import com.feiyuan.book.webmagic.bqg.processor.BookInfoProcessor;
import com.feiyuan.common.exception.RRException;
import com.feiyuan.common.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyuan.common.utils.PageUtils;
import com.feiyuan.common.utils.Query;

import com.feiyuan.book.dao.BookContentDao;
import com.feiyuan.book.entity.BookContentEntity;
import com.feiyuan.book.service.BookContentService;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Spider;


@Service("bookContentService")
public class BookContentServiceImpl extends ServiceImpl<BookContentDao, BookContentEntity> implements BookContentService {

    @Autowired
    private BookChapterService bookChapterService;

    @Autowired
    private BookListService bookListService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BookContentEntity> page = this.page(
                new Query<BookContentEntity>().getPage(params),
                new QueryWrapper<BookContentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public BookContentVO getBqgContent(BookChapterVO bookChapter) {
        String chapterUrl = bookChapter.getChapterUrl();
        if (StringUtils.isEmpty(chapterUrl)) {
            throw new RRException("参数不能为空!", 500);
        }

        if (!chapterUrl.contains(Constant.BQG_BASE_URL)) {
            chapterUrl = Constant.BQG_BASE_URL + chapterUrl;
        }
        BookChapterEntity chapter = bookChapterService.getOne(new QueryWrapper<BookChapterEntity>().eq("chapter_web_href", chapterUrl));
        if (chapter != null) {
            // 保存数据到数据库中
            BookContentEntity contentEntity = this.getOne(new QueryWrapper<BookContentEntity>().eq("chapter_id", chapter.getId()));
            if (contentEntity == null) {
                // 同步查询
                Spider spider = Spider.create(new BookContentProcessor());
                ResultItems result = spider.get(chapterUrl);
                if (result == null) {
                    throw new RRException("网络出错，请重试！", 500);
                }
                String content = result.get("content");
                BookContentVO bookContentVO = new BookContentVO();
                bookContentVO.setContentDetail(content);
                bookContentVO.setChapterId(chapter.getId());

                BookContentEntity bookContentEntity = new BookContentEntity();
                BeanUtils.copyProperties(bookContentVO, bookContentEntity);
                Date date = new Date();
                bookContentEntity.setCreateTime(date);
                bookContentEntity.setUpdateTime(date);
                this.save(bookContentEntity);

                return bookContentVO;
            } else {
                BookContentVO bookContentVO = new BookContentVO();
                bookContentVO.setContentDetail(contentEntity.getContentDetail());
                bookContentVO.setChapterId(contentEntity.getChapterId());
                return bookContentVO;
            }
        } else {
            throw new RRException("没有此章节!", 500);
        }
    }

    @Override
    public Map<String, Object> queryBqgChapterAndContent(BookContentVO bookContent) {
        Map<String, Object> map = new HashMap<>();
        // 1.查询书籍在数据库中的ID
        BookListEntity bookList = bookListService.getOne(new QueryWrapper<BookListEntity>().eq("book_web_id", bookContent.getBookWebId()));

        // 2.查询章节在数据库中的信息
        BookChapterEntity bookChapter = bookChapterService.getOne(new QueryWrapper<BookChapterEntity>().eq("book_id", bookList.getId()).eq("chapter_web_id", bookContent.getChapterWebId()));

        if (bookContent.getIsReturnChapter()) {
            // 3.获取章节列表
            List<BookChapterEntity> bookChapters = bookChapterService.list(new QueryWrapper<BookChapterEntity>().eq("book_id", bookList.getId()));
            map.put("chapters", bookChapters);
        }

        // 4.获取章节内容
        BookChapterVO bookChapterVO = new BookChapterVO();
        bookChapterVO.setChapterUrl(bookChapter.getChapterWebHref());
        BookContentVO bqgContent = this.getBqgContent(bookChapterVO);
        map.put("content", bqgContent);

        return map;
    }

}